#delimit;
capture log close;
set more off;
pause on;
clear all;
set scheme plottig;

cap cd "/Users/deandulay/Dropbox/Myanmar_JOP_EGI_replication";
cap cd "C:\Users\ejm5\Dropbox\Myanmar_JOP_EGI_replication";


/*Start with Raw MBEI Data from MBEI Project*/
use  "data\20200419_mbei_panel_final.dta", clear;

/*Merge in Data from where MBEI workshops took place (see facilitation workshops report*)*/
merge m:1 p_code using "data\workshop.dta";
replace workshop=0 if workshop==.;
lab var workshop "Held Facilitation Workshop=1";
label define workshop  1 "Facilitation Workshop" 0 "No Facilitation";
label values workshop workshop;

/*MERGE in MBEI Made of Only New 2020 Indicators*/
drop _merge;
merge 1:1 Panel_ID year using "data\20220324_MBEI_NewIndicators.dta";

/*MERGE in Indices Made Only of DAO and GAD Indicators*/
drop _merge;
merge 1:1 Panel_ID year using "data\20210807_mbei_panel_DAOGAD.dta";

/*Apply MBEI Survey Weights used in 2018 and 2020: Note the additions to address certainty units (capital townsips) and clustering at township level (PSU), as well as poststratification weights to address non-response
https://methods.sagepub.com/dataset/howtoguide/clustering-in-finaccess-2015*/

lab var PSW_FPC1 "Survey weights: Finite Population Correction";
lab var PSU "Survey weights: Primary Sampling Unit (Clustered Variable - Township)";
lab var PSW_STRATA "Survey weights: Stratification";

svyset PSU [pweight=weight_township],  fpc(PSW_FPC1) strata(PSW_STRATA)  poststrata(town_type) postweight(count_ttype) vce(linearized) singleunit(certainty) || _n;

/******************************************************************************************************************************************************/
/*Cleaning, Variable Generation, and Recoding*/

encode p_code, gen(p_code_new);
gen university_grad=1 if q17==6 | q17==7;
replace university_grad=0 if university_grad==.;
label var university_grad "Owner a univeristy grad";

gen bamar=1 if q20_1==86;
replace bamar=0 if bamar==.;
lab var bamar "Bamar ethnicity";

gen years_firm=2020-q9;
lab var years_firm "Years in operation";

replace q19=0 if q19==2;
label var q19 "Gender of owner (male)";

label var subindex10_final "Subindex 10: Law and Order";

gen manufacturing_firm=1 if broad_sector1=="C";
replace manufacturing_firm=0 if manufacturing_firm==.;
label var manufacturing_firm "Manufacturing Firm";

gen equity_above_500=1 if t_q108_2!=1 & t_q108_2!=2;
replace equity_above_500=0 if equity_above_500==.;
lab var equity_above_500 "Equity above 500 Lakh";

/*Label key dependent variable - unweighted index*/
label var unweighted "Aggregate Unweighted MBEI Index"; 

/*Label control group - unidirectional delivery*/
generate unidirectional=1 if workshop==0;
replace unidirectional=0 if workshop==1;


egen MBEI_survey=rowtotal(subindex1_survey subindex2_survey subindex3_survey subindex4_survey subindex5_survey subindex6_survey subindex7_survey subindex8_survey subindex9_survey subindex10_survey);
lab var MBEI_survey "MBEI_survey with only survey data";


egen MBEI_admin=rowtotal(subindex1_hard  subindex3_hard subindex4_hard subindex5_hard subindex6_hard  subindex8_hard subindex9_hard subindex10_hard);
lab var MBEI_admin "MBEI_survey with only administrative and observational data";

/*Recode Capital Townships*/
#delimit;
drop capital;
generate capital=1 if p_code=="MMR007001"|p_code=="MMR011001"|p_code=="MMR004002"|p_code=="MMR012001"|p_code=="MMR014001"|p_code=="MMR002001"|
p_code=="MMR003003"|p_code=="MMR005012"|p_code=="MMR010004"|p_code=="MMR009001"|p_code=="MMR001001"|p_code=="MMR017001"|
p_code=="MMR018007"|p_code=="MMR006001"|p_code=="MMR013008";
replace capital=0 if capital==.;


drop rescale* q82_binary q83_binary q84_binary q85_binary q90_binary t_q46_1_binary t_q46_3_binary t_q46_4_binary t_q46_7_binary t_q46_5_binary t_q46_10_binary t_q112_1_binary t_q112_2_binary t_q112_3_binary t_q112_4_binary t_q112_5_binary t_q112_6_binary  t_q112_8_binary t_q112_9_binary t_q113_1_binary t_q113_2_binary t_q113_3_binary q142_binary q114_binary q116_binary t_q60_1_binary t_q60_2_binary t_q60_3_binary t_q60_4_binary t_q60_5_binary q86_binary q87_binary q124_binary t_q128_1_binary t_q128_2_binary t_q128_3_binary t_q128_4_binary t_q128_6_binary q129_binary change_unweighted labor_size_y3 labor_size_y2 labor_size_y1 labor_growth capital_growth capital_growth expand satisfaction_state satisfaction_town satisfaction_gsd satisfaction_dc satisfaction_local w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 ln_labor _merge sector_broad;

lab var Q110_1n "Name of Other Lender";
lab var Q110_1_C "Loan number";
lab var subindex4_hard "Subindex 4: Hard Data";
lab var subindex5_hard "Subindex 5: Hard Data";
lab var subindex6_hard "Subindex 6: Hard Data";
lab var broad_sector1 "ISIC First Digit Letter - Broad Sector";
lab var isic_2 "Indidustrial Standard Classification (ISIC-Rev4) 4-digit code";
lab var subindex2_hardNEW "Land Access & Secturity Sub-Index Hard Data New";
lab var subindex3_hardNEW "Post-Entry Regulation Sub-Index Hard Data New";
lab var subindex5_hardNEW "Infrastructure Sub-Index Hard Data New";
lab var subindex6_hardNEW "Transparency Sub-Index Hard Data New";
lab var subindex8_hardNEW "Environmental Compliance Sub-Index Hard Data New";
lab var subindex9_hardNEW "Labor Recruitment Sub-Index Hard Data New";
lab var subindex10_hardNEW "Law & Order Sub-Index Hard Data New";
lab var unidirectional "Control Group=1; Only received unidirectional report";
lab var weighted_MBEI "Official Weighted MBEI Presented to Myanmar Government in 2019 and 2020";
lab var capital "Capital of township==1";
lab var t_q112_7_binary " Accesibility of Land use allocation plans and maps (1=Accessible)";

order Panel_ID year state_region township p_code q214 PSW_FPC1 PSU PSW_STRATA weight_township workshop unidirectional unweighted weighted_MBEI  unweighted_NEW unweighted_HARD_NEW MBEI_survey MBEI_admin DAO_indicators_final DAO_indicators_hard DAO_indicators_soft DAO_finalNEW DAO_surveyNEW DAO_hardNEW GAD_indicators_final GAD_indicators_hard GAD_indicators_soft  GAD_finalNEW GAD_surveyNEW GAD_hardNEW subindex1_final subindex2_final subindex3_final subindex4_final subindex5_final subindex6_final subindex7_final subindex7_final subindex8_final subindex9_final subindex10_final;
/******************************************************************************************************************************************************/
/*Save Working Data*/
save  "data\20220606_mbei_panel_working.dta", replace;


/*Create Codebook*/
codebookout "JOP_MBEI_ReplicationData_Codebook", replace;
/******************************************************************************************************************************************************/



/**********************************************************************************************************************/
/*COLLAPSING TO TOWNSHIP LEVEL USING SURVEY WEIGHTS TO ANALYSE TOWNSHIP LEVLEL FULL AND ADMINISTRATIVE DATA**/
#delimit;
by year, sort: sum unweighted if township==49;

#delimit;
foreach x in weighted unweighted subindex1_final subindex2_final subindex3_final subindex4_final subindex5_final subindex6_final subindex7_final subindex8_final subindex9_final subindex10_final DAO_surveyNEW DAO_finalNEW GAD_surveyNEW GAD_finalNEW DAO_indicators_soft DAO_indicators_final GAD_indicators_soft GAD_indicators_final  {;
svy: mean `x', over(township year);
mat list e(b);
mat `x'_w=e(b)';
};

#delimit;
collapse q9 workshop  literacy surface_area lag  MBEI_admin unweighted_HARD_NEW GAD_hardNEW DAO_hardNEW DAO_indicators_hard GAD_indicators_hard (median) state_region, by(township capital year);

#delimit;
foreach x in weighted unweighted subindex1_final subindex2_final subindex3_final subindex4_final subindex5_final subindex6_final subindex7_final subindex8_final subindex9_final subindex10_final DAO_surveyNEW DAO_finalNEW GAD_surveyNEW GAD_finalNEW DAO_indicators_soft DAO_indicators_final GAD_indicators_soft GAD_indicators_final  {;
svmat `x'_w, names(matcol);
};

#delimit;
replace unweighted_wy1=56.21 if township==49 & year==2018;
xtset township year;
gen diff_unweighted= unweighted_wy1 -l2.unweighted_wy1;
sum diff_unweighted;
gsort - unweighted_w;
by year, sort: generate rank=_n;


/**********************************************************************************************************************/
/*One township dropped due to few firms and survey weights*/
/**********************************************************************************************************************/
/*************************************************************************************************************************************************************************************************************/

/******************************************************************************************************************************************************/
/*Save Township Data*/
save  "data\20220606_mbei_township.dta", replace;
/******************************************************************************************************************************************************/


/**********************************************************************************************************************/
/*COLLAPSING TO STATE & Region LEVEL**/
/**********************************************************************************************************************************/
#delimit;
collapse weighted_wy1 unweighted* capital workshop literacy surface_area lag, by(state_region year);
xtset state_region year;
generate fd_weighted=((weighted-l2.weighted)/l2.weighted)*100;
lab var fd_weighted "Change in Weighted Index (%)";
replace workshop=workshop*100;
lab var workshop "Share of Treated Townships in State";
label variable state_region "State or region of main operation";
label values state_region q10;
by state_region, sort: gen count=_n;
by state_region, sort: egen max_count=max(count);
drop if max_count<2;

/******************************************************************************************************************************************************/
/*Save STATE Data*/
save  "data\20220606_mbei_state.dta", replace;
/******************************************************************************************************************************************************/




























